let app = getApp()
Component({
    properties: {
 
      },
      data:{
        siteDomain:app.globalData.siteDomain,
        title: "主播入驻申请",
        liveclose:0,
        formList: [ {
            validate: {
                rule: /^\S+$/,
                msg: "请输入主播昵称"
            },
            value: "",
            name: "anchor_name",
            title: "主播昵称",
            type: "input",
            placeholder: "请输入主播昵称",
            hiddenArrow: !0
        }, {
            validate: {
                rule: /^\S+$/,
                msg: "请输入主播微信号"
            },
            value: "",
            name: "anchor_wechat",
            title: "主播微信号",
            type: "input",
            placeholder: "请输入主播微信号",
            hiddenArrow: !0
        }, {
            validate: {
                rule: /^1\d{10}$/,
                msg: "请输入正确手机号"
            },
            value: "",
            name: "mobile",
            title: "手机号",
            type: "input",
            placeholder: "请输入手机号",
            hiddenArrow: !0
        } ],
        checkCodeText: "获取验证码",
        agreementStatus: !1,
        applyStatus: 0,
        errorRemark: "",
        liveSetting: {},
        firstEnter: !0
      },
    lifetimes: {
        attached: function () {
          var that=this
          wx.request({
            url: app.globalData.siteroot,
            data: {
              do:'LivePlayer_getLivePlayerSetting',
              user_id:wx.getStorageSync('user_id'),
            },
            success: function(res) {
              if(res.data.code==400){
                wx.showToast({
                  title:res.data.msg,
                  icon: "none"
                });
                that.setData({
                  msg:res.data.msg,
                  liveclose:1
                })
              }else{
                var formList=that.data.formList
                formList[0]['value']=res.data.data.live_player_anchor.anchor_name
                formList[1]['value']=res.data.data.live_player_anchor.anchor_wechat
                formList[2]['value']=res.data.data.live_player_anchor.mobile
                that.setData({
                  liveSetting:res.data.data.live_player_setting,
                  firstEnter:!res.data.data.live_player_anchor.anchor_name,
                  applyStatus:res.data.data.live_player_anchor.status,
                  formList:formList
                 })
                
                 if(res.data.data.live_player_setting.is_sms==1){
                     var sms={
                      value: "",
                      name: "code",
                      title: "验证码",
                      type: "input",
                      placeholder: "请填写验证码",
                      hiddenArrow: !0
                   }
                   
                   formList.push(sms)
                   that.setData({
                    formList:formList
                   })
                 }
              }
            }
          })
       }
      },
    methods: {
      getcode:function(){
        var formList=this.data.formList
        var mobile=formList[2].value
        if(this.data.checkCodeText!='获取验证码'){
                return false;
        }
        var myreg=/^1\d{10}$/
        if (!myreg.test(mobile)) {
          wx.showToast({
            title:formList[2].validate.msg,
            icon: "none"
          });
        }else{
          var that=this
          wx.request({
            url: app.globalData.siteroot,
            data: {
              do:'sms_sendVerificationCode',
              phone:mobile
            },
            success: function(res) {
              that.startSetInter();
                  if(res.data.code==0){
                    wx.showToast({
                      title:res.data.msg,
                      icon: "none"
                    });
                  }else{
                    wx.showToast({
                      title:'发送成功',
                      icon: "none"
                    });
                    that.setData({
                      checkCodeText:60
                    })
                    that.startSetInter();
                  }
            }
          })
        }
      },
      startSetInter: function(){
        var that = this;
        //将计时器赋值给setInter
        that.data.setInter = setInterval(
            function () {
                var numVal = that.data.checkCodeText-1;
                if(numVal>0){
                  that.setData({ checkCodeText: numVal });
                }else{
                  that.endSetInter();
                }
            }
      , 1000);   
    },
    endSetInter: function(){
        var that = this;
        //清除计时器  即清除setInter
        clearInterval(that.data.setInter)
        that.setData({
          checkCodeText:'获取验证码'
        })
    },
      apply:function(){
        if(this.data.liveclose==1){
          wx.showToast({
            title:this.data.msg,
            icon: "none"
          });
          return false;
        }
        if(this.data.agreementStatus==!1){
          wx.showToast({
            title:'请先阅读用户须知与主播协议',
            icon: "none"
        });
        }else{
          var formList=this.data.formList
          for(var i=0;i<formList.length;i++){
               if(formList[i].value==''){
                wx.showToast({
                  title:formList[i].placeholder,
                  icon: "none"
                 });
                 return false;
             }
          }
          var mobile=formList[2].value
          var myreg=/^1\d{10}$/
          if (!myreg.test(mobile)) {
            wx.showToast({
              title:formList[2].validate.msg,
              icon: "none"
            });
            return false;
          }
          var that=this
          if(that.data.liveSetting.is_sms==1){
            wx.request({
              url: app.globalData.siteroot,
              data: {
                do:'sms_checkVerificationCode',
                phone:formList[2].value,
                code:formList[3].value
              },
              success: function(res) {
                 if(res.data.code==0){
                  wx.showToast({
                    title:res.data.msg,
                    icon: "none"
                  });
                 }else{
                  wx.request({
                    url: app.globalData.siteroot,
                    data: {
                      do:'LivePlayer_applyLivePlayerAnchor',
                      user_id:wx.getStorageSync('user_id'),
                      anchor_name:formList[0].value,
                      anchor_wechat:formList[1].value,
                      mobile:formList[2].value,
                    },
                    success: function(res) {
                      if(res.data.code==0){
                        wx.showToast({
                          title:res.data.msg,
                          icon: "none"
                       });
                      }else{
                        wx.showToast({
                          title:'入驻成功',
                          icon: "none"
                       });
                       formList[3].name='end'
                       that.setData({
                        formList:formList,
                        applyStatus:1
                       })
                      }
                     
                    }
                  })
                 }
              }
            })
          }else{
            wx.request({
              url: app.globalData.siteroot,
              data: {
                do:'LivePlayer_applyLivePlayerAnchor',
                user_id:wx.getStorageSync('user_id'),
                anchor_name:formList[0].value,
                anchor_wechat:formList[1].value,
                mobile:formList[2].value,
              },
              success: function(res) {
                if(res.data.code==0){
                  wx.showToast({
                    title:res.data.msg,
                    icon: "none"
                 });
                }else{
                  wx.showToast({
                    title:'入驻成功',
                    icon: "none"
                 });
                 formList[3].name='end'
                 that.setData({
                  formList:formList,
                  applyStatus:1
                 })
                }
               
              }
            })
          }
         
        }
      },
      inputval:function(e){
          var index=e.currentTarget.dataset.index
          var formList=this.data.formList
          formList[index]['value']=e.detail.value
      },
      agreenent:function(){
        wx.navigateTo({ 
          url: '/yb_shopv2/native/all/index?type=live_agreement',
       })
      },
      agree:function(){
          if(this.data.agreementStatus==!1){
                this.setData({
                  agreementStatus:1
                })
          }else{
            this.setData({
              agreementStatus:!1
            })
          }
      }
    }
});
